﻿module Sustainalytics.GCCS.Security {

    export class GCPermissionsService implements IGCPermissionsService {

        constructor(
            private $q: ng.IQService,
            private ClaimsAuthorizationService: Sustainalytics.Claims.ClaimsAuthorizationService) {
        }

        public getPermissions(): GCPermissions {
            var hasNC = this.hasNC();
            var hasWL = this.hasWL();
            var hasMultiCompNC = this.hasMultiCompNC();
            var hasMultiCompWL = this.hasMultiCompWL();

            var permissions = new GCPermissions(hasNC, hasWL, hasMultiCompNC, hasMultiCompWL);
            return permissions;
        }

        hasNC(): boolean {
            var ctx = this;

            var params: Sustainalytics.Claims.ResourceParameters[] = new Array();
            var paramOverallAssessment = new Sustainalytics.Claims.ResourceParameters();
            paramOverallAssessment.key = 'overallAssessment';
            paramOverallAssessment.value = GCAssessment.NonCompliant.toString();
            params.push(paramOverallAssessment);

            var valid = ctx.ClaimsAuthorizationService.Validate('api/v1/gcoverallcompanysummary', 'GET', params);
            return valid;
        }

        hasWL(): boolean {
            var ctx = this;

            var params: Sustainalytics.Claims.ResourceParameters[] = new Array();
            var paramOverallAssessment = new Sustainalytics.Claims.ResourceParameters();
            paramOverallAssessment.key = 'overallAssessment';
            paramOverallAssessment.value = GCAssessment.Watchlist.toString();
            params.push(paramOverallAssessment);

            var valid = ctx.ClaimsAuthorizationService.Validate('api/v1/gcoverallcompanysummary', 'GET', params);
            return valid;
        }

        hasMultiCompNC(): boolean {
            var ctx = this;

            var params: Sustainalytics.Claims.ResourceParameters[] = new Array();
            var paramOverallAssessment = new Sustainalytics.Claims.ResourceParameters();
            paramOverallAssessment.key = 'overallAssessment';
            paramOverallAssessment.value = GCAssessment.NonCompliant.toString();
            params.push(paramOverallAssessment);

            var valid = ctx.ClaimsAuthorizationService.Validate('api/v1/gcoverallcompanysummaryextended', 'GET', params);
            return valid;
        }

        hasMultiCompWL(): boolean {
            var ctx = this;

            var params: Sustainalytics.Claims.ResourceParameters[] = new Array();
            var paramOverallAssessment = new Sustainalytics.Claims.ResourceParameters();
            paramOverallAssessment.key = 'overallAssessment';
            paramOverallAssessment.value = GCAssessment.Watchlist.toString();
            params.push(paramOverallAssessment);

            var valid = ctx.ClaimsAuthorizationService.Validate('api/v1/gcoverallcompanysummaryextended', 'GET', params);
            return valid;
        }
    }


    angular.module(gccsSecurityModuleName).service('GCPermissionsService',
        ['$q', 'ClaimsAuthorizationService', GCPermissionsService]);
} 